package com.example.cdctravel

import android.annotation.SuppressLint
import android.content.Intent
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.view.KeyEvent
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.cdctravel.DataBaseHelper.Companion.getInstance
import com.example.cdctravel.MainActivity

class MainChangemima : AppCompatActivity() {
    private var db: SQLiteDatabase? = null
    private var helper: DataBaseHelper? = null
    private var main_Gzhanghao: EditText? = null
    private var main_Oldmima: EditText? = null
    private var main_Newmima: EditText? = null
    private var main_Gquxiao: Button? = null
    private var main_Gqueding: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_changemima)
        intiview()
        main_Gquxiao!!.setOnClickListener {
            val intent = Intent(this@MainChangemima, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        main_Gqueding!!.setOnClickListener {
            val zhanghao = main_Gzhanghao!!.text.toString()
            val oldmima = main_Oldmima!!.text.toString()
            val newmima = main_Newmima!!.text.toString()
            if (zhanghao == "") {
                Toast.makeText(this@MainChangemima, "账号不能为空", Toast.LENGTH_SHORT).show()
            } else {
                if (oldmima == "" && newmima == "") {
                    Toast.makeText(
                        this@MainChangemima,
                        "新密码或旧密码不能为空",
                        Toast.LENGTH_SHORT
                    ).show()
                } else {
                    val cursor: Cursor
                    cursor =
                        db!!.rawQuery("select password from users where name=?", arrayOf(zhanghao))
                    if (cursor.moveToNext()) {
                        @SuppressLint("Range") val true_password =
                            cursor.getString(cursor.getColumnIndex("password"))
                        cursor.close()
                        if (true_password == oldmima) {
                            db!!.execSQL(
                                "update users set password=? where name=?",
                                arrayOf(newmima, zhanghao)
                            )
                            val intent = Intent(this@MainChangemima, MainActivity::class.java)
                            rememberMe(null, null, false)
                            startActivity(intent)
                            finish()
                        } else Toast.makeText(
                            this@MainChangemima,
                            "旧密码错误$true_password",
                            Toast.LENGTH_SHORT
                        ).show()
                    } else Toast.makeText(
                        this@MainChangemima,
                        "无此账号,先去注册一个吧",
                        Toast.LENGTH_SHORT
                    ).show()
                    cursor.close()
                }
            }
        }
    }

    private fun intiview() {
        helper = getInstance(this)
        db = helper!!.writableDatabase
        main_Gzhanghao = findViewById(R.id.main_gzhanghao)
        main_Oldmima = findViewById(R.id.main_oldmima)
        main_Newmima = findViewById(R.id.main_newmima)
        main_Gquxiao = findViewById(R.id.main_gquxiao)
        main_Gqueding = findViewById(R.id.main_gqueding)
    }

    fun rememberMe(name1: String?, password1: String?, auto1: Boolean) {
        val sp = getSharedPreferences(SPdata, MODE_PRIVATE)
        val editor = sp.edit() //获得Editor
        editor.putString(SPname, name1) //将用户的帐号存入Preferences
        editor.putString(SPpassword, password1) //将密码存入Preferences
        editor.putBoolean(SPauto, auto1)
        editor.commit()
    }

    //按两次back键退出
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        //获取按键并比较两次按back的时间大于2s不退出，否则退出
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == 0) {
            val intent = Intent(this@MainChangemima, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object {
        private const val SPdata = "SPdata"
        private const val SPname = "name"
        private const val SPpassword = "password"
        private const val SPauto = "auto"
    }
}